package com.service.infoEntry.controller;

import com.service.config.utils.*;
import com.service.infoEntry.dto.TroubleInfoDto;
import com.service.infoEntry.dto.UserDto;
import com.service.infoEntry.entity.TroubleInfo;
import com.service.infoEntry.service.TeamService;
import com.service.infoEntry.service.TroubleService;
import com.service.infoEntry.vo.TroubleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import static com.service.infoEntry.constant.Constant.*;

/**
 * 问题controller
 */
@CrossOrigin
@RestController()
@RequestMapping("/trouble")
@Api(value = "问题相关接口")
public class TroubleController {
    @Resource
    RedisUtil redisUtil;
    @Resource
    TroubleService troubleService;
    @Resource
    TeamService teamService;

    @ApiOperation(value = "记录问题")
    @RequestMapping(value ="/saveTrouble",method = RequestMethod.POST)
    @ResponseBody
    public Result saveTrouble(@RequestBody TroubleInfoDto troubleInfoDto,
                               @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {

                troubleInfoDto.setUserId(userId);
                return troubleService.addTrouble(troubleInfoDto,userId);
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "问题跟踪")
    @RequestMapping(value ="/updateTrouble",method = RequestMethod.POST)
    @ResponseBody
    public Result updateTrouble(@RequestBody TroubleInfoDto troubleInfoDto,
                               @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                troubleService.updateTrouble(troubleInfoDto);
                return Result.ok();
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "问题关闭")
    @RequestMapping(value ="/closeTrouble",method = RequestMethod.POST)
    @ResponseBody
    public Result closeTrouble(@RequestBody TroubleInfoDto troubleInfoDto,
                                @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                troubleService.updateTrouble(troubleInfoDto);
                return Result.ok();
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 基于单号的精确搜索
     * @param no 订单号
     * @param cloudAuthToken
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "基于单号精确搜索")
    @RequestMapping(value ="/searchTroubleByNo",method = RequestMethod.GET)
    public Result searchTroubleByNo(@RequestParam("no")String no,
                               @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                TroubleVo troubleVo = troubleService.searchTroubleByNo(no,userId);
                return Result.ok().setData(troubleVo);
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 基于描述的模糊搜索
     * @param desc 订单号
     * @param cloudAuthToken
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "基于描述的模糊搜索")
    @RequestMapping(value ="/searchTroubleByDesc",method = RequestMethod.GET)
    public Result searchTroubleByDesc(@RequestParam("desc")String desc,
                                      @RequestParam("teamId")String teamId,
                                    @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                List<TroubleVo> troubleVoList = troubleService.searchTroubleByDesc(desc,userId, teamId);
                return Result.ok().setData(troubleVoList);
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "查询某个项目下的所有问题")
    @RequestMapping(value ="/queryTroubleOfProgramme",method = RequestMethod.GET)
    @ResponseBody
    public Result queryTroubleOfProgramme(@RequestParam("programmeId") String programmeId,
                                          @RequestParam("teamId") String teamId,
                                          @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                List<TroubleVo> troubles = troubleService.queryTroubleOfProgramme(programmeId,userId,teamId);
                return Result.ok().setData(troubles);
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "导出问题清单")
    @RequestMapping(value ="/exportTroubleList",method = RequestMethod.GET)
    @ResponseBody
    public Result exportTroubleList(/*@RequestParam("programmeId") String programmeId,*/
                                    @RequestParam("teamId") String teamId,
                                          @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                String filePath = troubleService.exportTroubleList(teamId,userId);
                return Result.ok().setData(filePath);
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    public Integer checkUserAuthority(String userId, String teamId)
    {
        Integer role = teamService.checkUserAuthority(userId,teamId);
        return role;
    }
}
